using System;
using System.Linq;
using System.Xml.Linq;
using Granite.DataAnnotations;
using Granite.Modeling;
using Toon.Models.Catalogs;


namespace Toon.Models.Characters
{
    public class Shitck : ChangeTrackingModelBase
	{
		readonly ShitckDefinition m_Definition;
		public Shitck(ShitckDefinition definition)
		{
			m_Definition = definition;
		}

		public string Name
		{
			get { return Get<string>(m_Definition.Name, "Name"); }
			set { Set(value, "Name"); }
		}
		public string BookLocation
		{
			get { return m_Definition.BookLocation; }
		}

		public bool UsableOnOthers
		{
			get { return Get<bool>("UsableOnOthers"); }
			set { Set(value, "UsableOnOthers"); }
		}

		/// <summary>
		/// Special rules that affect the shitck for this character, usually a limitation.
		/// </summary>
		public string Notes
		{
			get { return Get<string>("", "Notes"); }
			set { Set(value, "Notes"); }
		}

		/// <summary>
		/// Adjustment to the skill point cost for tweaks.
		/// </summary>
		public int TweakPoints
		{
			get { return Get<int>("TweakPoints"); }
			set { Set(value, "TweakPoints"); }
		}

		/// <summary>
		/// This tracks the number of skill points spent.
		/// </summary>
		[CalculatedField("TweakPoints")]
		[CalculatedField("UsableOnOthers")]
		public int SkillPoints
		{
			get { return TweakPoints + m_Definition.BaseCost + (UsableOnOthers ? 2 : 0); }
		}

		/// <summary>
		/// This tracks the number of plot points spent.
		/// </summary>
		public int PlotPoints
		{
			get { return Get<int>("PlotPoints"); }
			set { Set(value, "PlotPoints"); }
		}

		[CalculatedField("PlotPoints")]
		public int SkillLevel
		{
			get { return (PlotPoints / 4) + 5; }
		}

        public static Shitck FromXml(XElement source)
        {
            var result = new Shitck(new ShitckDefinition(source.Attribute("Name").Value, int.Parse(source.Attribute("BaseCost").Value), source.Attribute("BookLocation").Value));
            result.UsableOnOthers = bool.Parse(source.Attribute("UsableOnOthers").Value);
            result.Notes = source.Attribute("Notes").Value;
            result.TweakPoints = int.Parse(source.Attribute("TweakPoints").Value);
            result.PlotPoints = int.Parse(source.Attribute("PlotPoints").Value);
            return result;
        }

        internal XElement ToXml()
        {
            return new XElement("Shitck", 
                       new XAttribute("Name", Name),
                       new XAttribute("BookLocation", BookLocation),
                       new XAttribute("BaseCost", m_Definition.BaseCost), 
                       new XAttribute("UsableOnOthers", UsableOnOthers), 
                       new XAttribute("Notes", Notes), 
                       new XAttribute("TweakPoints", TweakPoints), 
                       new XAttribute("PlotPoints", TweakPoints));
        }

        public override string ToString()
        {
            return string.Format("{0} {1}", Name, SkillLevel);
        }
    }
}
